package com.changanw.dd.aspect;

import com.changanw.dd.datasource.DataSourcetype;
import com.changanw.dd.datasource.DynamicDataSourceContextHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 *
 */
@Aspect
@Component
@Order(10)
public class GloalDataSourceAspect {

    @Autowired
    HttpSession session;

    @Pointcut("execution(* com.changanw.dd.service.*.*(..))")
    public void pc() {

    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint pjp) {
        DynamicDataSourceContextHolder.setDatasourceType((String) session.getAttribute(DataSourcetype.DS_SESSION_KEY));
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            DynamicDataSourceContextHolder.clearDataSource();
        }
        return null;
    }
}
